package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.springboot.service.INoticeService;
import com.example.springboot.entity.Notice;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2023-09-09
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private INoticeService noticeService;

    //新增和修改
    @HoneyLogs(operation = "公告",type = LogType.ADD)
    @PostMapping
    public Result save(@RequestBody Notice notice) {  //@RequestBody接收前端数据
        if (notice.getId() == null) {
            notice.setTime(DateUtil.now());
            notice.setUser(TokenUtils.getCurrentUser().getNickname());
        }
        noticeService.save(notice);
        return Result.success();
    }
    @HoneyLogs(operation = "公告",type = LogType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody Notice notice) {  //@RequestBody接收前端数据

        noticeService.updateById(notice);
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(noticeService.list());
    }

    //根据类型查询公告
    @GetMapping("/type/{type}")
    public Result findType(@PathVariable Integer type) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        return Result.success(noticeService.list(queryWrapper));
    }


    //删除
    @HoneyLogs(operation = "公告",type = LogType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(noticeService.removeById(id));
    }

    //批量删除
    @HoneyLogs(operation = "公告",type = LogType.BATCH_DELETE)
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(noticeService.removeByIds(ids));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(noticeService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam String type,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("type", type);
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(noticeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}



